﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using BO;
using Channelvn.Cached.Common;
using EntityFramework;
using Rewrite;

namespace ProjectQuaDep.Controls
{
    public partial class ListProduct : System.Web.UI.UserControl
    {
        protected string urlZone = "";
        protected string titleZone = "";
        int PageIndex = lib.PageIndex;
        int PageSize = 10;
        protected bool noItem = true;
        private string sFormatNews = @"<li><a href=""{0}""><img src=""{1}"" /></a><h3><a href=""{0}"">{2}</a></h3><p>{3}</p></li>";
        protected void Page_Load(object sender, EventArgs e)
        {
            int zoneID = BO.lib.ZoneID;
            var catUrl = Lib.QueryString.CatUrl;
            var pageIndex = Lib.QueryString.PageIndex;
            var pageType = Lib.QueryString.PageType;
            //urlZone = BO.ReWriteUrl.BuildLink.BuildLinkZoneNews(zoneID.ToString());
            //DataTable dtZone = BO.News.ZoneBO.GetDetailZoneBO(zoneID.ToString());
            //if (dtZone != null && dtZone.Rows.Count > 0)
            //{
            //    DataRow dr = dtZone.Rows[0];
            //    titleZone = dr[BO.Const.tbZoneNews.title].ToString();
            //    //
            //    //set meta
            //    string Trang = "";
            //    if (PageIndex > 1)
            //    {
            //        Trang = " | Trang " + PageIndex;
            //    }
            //    Page.Title = dr[Const.tbZoneNews.title].ToString() + Trang;
            //    string hDesc = dr[Const.tbZoneNews.title].ToString() + Trang;
            //    string hKey = dr[Const.tbZoneNews.title].ToString();
            //    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
            //    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
            //    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
            //}

            if (string.IsNullOrEmpty(catUrl)) _301Redirection.RedirectTo("/404.aspx");
            ZoneProduct obj;
            if (pageType == 7) // List Product Company
            {
                obj = ZoneProductBO.GetZoneProductById(zoneID);
                if (obj != null)
                {
                    #region nav
                    {
                        var sb = new StringBuilder();
                        sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);
                        titleZone = sb.ToString();
                    }
                    #endregion

                    //set meta
                    string Trang = "";
                    if (PageIndex > 1)
                    {
                        Trang = " | Trang " + PageIndex;
                    }
                    Page.Title = obj.title + Trang;
                    string hDesc = obj.title + Trang;
                    string hKey = obj.title;
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
                }
            }
            else
            {
                obj = ZoneProductBO.GetZoneProductByCatUrl(catUrl);
                if (obj != null)
                {
                    #region nav

                    {
                        var sb = new StringBuilder();
                        if (obj.ParentId != null && obj.ParentId > 0)
                        {
                            var objChild = ZoneProductBO.GetZoneProductById(obj.ParentId.Value);
                            if (objChild != null)
                                sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a> > ", objChild.url, HttpUtility.HtmlEncode(objChild.title), objChild.title);
                        }
                        sb.AppendFormat("<a href='/{0}.htm' title='{1}' >{2}</a>", obj.url, HttpUtility.HtmlEncode(obj.title), obj.title);

                        ZoneProduct zd = ZoneProductBO.GetZoneProductDetailByUrl(catUrl);
                        titleZone = sb.ToString();
                        zoneID = obj.ZoneId;

                        //set meta
                        string Trang = "";
                        if (PageIndex > 1)
                        {
                            Trang = " | Trang " + PageIndex;
                        }
                        Page.Title = obj.title + Trang;
                        string hDesc = obj.title + Trang;
                        string hKey = obj.title;
                        BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                        BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                        BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
                    }
                    #endregion


                }
            }

            #region list product
            {
                var lst = ProductsBO.GetPagingProductByZoneProduct(pageIndex, PageSize, zoneID);
                if (lst.Any())
                {
                    noItem = false;
                    var sb = new StringBuilder();
                    var count = 1;

                    rptData.DataSource = lst;
                    rptData.DataBind();

                    if (lst.Count > 0)
                    {
                        //ltrListFocus.Text = sb.ToString();
                    }
                    else
                    {
                        //ltrListFocus.Text = "<h1>Hiện chưa có sản phẩm nào ở danh mục này</h1>";
                    }
                }
            }
            #endregion

            #region paging
            this.PaddingNews1.PageSize = PageSize;
            this.PaddingNews1.SumPage = ProductsBO.GetPagingNumber(zoneID, PageSize);

            this.PaddingNews1.Visible = !noItem ? true : false;

            this.PaddingNews1.LinkSite = "/" + Lib.QueryString.url + "/";
            this.PaddingNews1.Padding(pageIndex);
            #endregion
        }
    }
}